﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Drawing.Printing;


namespace Mobileterminal.Forms.Zyy
{
    public class AdvancedPrintForm : Form
    {
        private Button btnPrint = new Button();
        private Button btnPreview = new Button();
        private Button btnPageSetup = new Button();
        private DataGridView dataGridView1 = new DataGridView();
        private PrintDocument printDocument = new PrintDocument();
        private PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
        private PrintDialog printDialog = new PrintDialog();
        private PageSetupDialog pageSetupDialog = new PageSetupDialog();

        public AdvancedPrintForm()
        {
            // 初始化UI
            btnPrint.Text = "打印";
            btnPrint.Click += BtnPrint_Click;

            btnPreview.Text = "预览";
            btnPreview.Click += BtnPreview_Click;

            btnPageSetup.Text = "页面设置";
            btnPageSetup.Click += BtnPageSetup_Click;

            // 添加控件到窗体
            TableLayoutPanel panel = new TableLayoutPanel();
            panel.Dock = DockStyle.Top;
            panel.Controls.Add(btnPageSetup, 0, 0);
            panel.Controls.Add(btnPreview, 1, 0);
            panel.Controls.Add(btnPrint, 2, 0);

            dataGridView1.Dock = DockStyle.Fill;

            this.Controls.Add(panel);
            this.Controls.Add(dataGridView1);

            // 初始化DataGridView
            InitializeDataGridView();

            // 配置打印文档
            printDocument.PrintPage += PrintDocument_PrintPage;
            printDialog.Document = printDocument;
            printPreviewDialog.Document = printDocument;
            pageSetupDialog.Document = printDocument;
        }

        private void InitializeDataGridView()
        {
            // 添加示例数据
            dataGridView1.Columns.Add("Applicationnumber", "申请单号");
            dataGridView1.Columns.Add("Applicationunit", "申请单位");
            //dataGridView1.Columns.Add("distributionunit", "发放单位");
            //dataGridView1.Columns.Add("Applicationtime", "申请时间");
            //dataGridView1.Columns.Add("Department", "部门");

            dataGridView1.Rows.Add("10032", "内科","消毒供应中心","2022-05-11");
            dataGridView1.Rows.Add("10033", "手术室", "消毒供应中心", "2023-05-11");
            dataGridView1.Rows.Add("10034", "手术室", "消毒供应中心", "2025-05-11");
            dataGridView1.Rows.Add("10035", "手术室", "消毒供应中心", "2025-05-11");

        }

        private void BtnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                if (printDialog.ShowDialog() == DialogResult.OK)
                {
                    printDocument.Print();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打印出错: {ex.Message}");
            }
        }

        private void BtnPreview_Click(object sender, EventArgs e)
        {
            printPreviewDialog.ShowDialog();
        }

        private void BtnPageSetup_Click(object sender, EventArgs e)
        {
            pageSetupDialog.ShowDialog();
        }

        private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            // 打印标题
            Font titleFont = new Font("Arial", 18, FontStyle.Bold);
            e.Graphics.DrawString("领用申请", titleFont, Brushes.Black,
                e.MarginBounds.Left, e.MarginBounds.Top - titleFont.GetHeight() - 20);

            // 打印DataGridView
            Bitmap bm = new Bitmap(dataGridView1.Width, dataGridView1.Height);
            dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, dataGridView1.Width, dataGridView1.Height));

            // 调整大小以适应页面
            Rectangle printArea = e.MarginBounds;
            float scale = Math.Min(
                (float)printArea.Width / (float)bm.Width,
                (float)printArea.Height / (float)bm.Height);

            int scaledWidth = (int)(bm.Width * scale);
            int scaledHeight = (int)(bm.Height * scale);

            e.Graphics.DrawImage(bm, printArea.Left, printArea.Top, scaledWidth, scaledHeight);

            e.HasMorePages = false;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // AdvancedPrintForm
            // 
            this.ClientSize = new System.Drawing.Size(804, 392);
            this.Name = "AdvancedPrintForm";
            this.Load += new System.EventHandler(this.AdvancedPrintForm_Load);
            this.ResumeLayout(false);

        }

        private void AdvancedPrintForm_Load(object sender, EventArgs e)
        {

        }
    }
}
